package alg20241023;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Text {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ret = new ArrayList<>();
        Arrays.sort(nums);
        int n = nums.length;
        for (int i = 0; i < n;) {
            int sum = 0;
            for (int j = i+1; j < n;) {
                int left = j+1;
                int right = n-1;
                long target1 = (long)target - nums[i] - nums[j];   //用long防止溢出

                while(left < right){
                    sum = nums[left] + nums[right];
                    if(sum > target1) right--;
                    else if(sum < target1) left++;
                    else{
                        ret.add(new ArrayList<>(Arrays.asList(nums[i],nums[j],nums[left],nums[right])));
                        //继续遍历找符合的元素
                        left++; right--;
                        while(left < right && nums[left-1] == nums[left]){
                            left++;
                        }
                        while(left < right && nums[right+1] == nums[right]){
                            right--;
                        }
                    }

                }
                j++;
                while(j < n && nums[j-1] == nums[j]){
                    j++;
                }

            }
            i++;
            while(i < n && nums[i-1] == nums[i]){
                i++;
            }
        }
        return ret;
    }
}
